package com.hankol.material.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baselibrary.bean.download.DownloadInfo
import com.baselibrary.db.DBManager
import com.baselibrary.utils.SpUtils


/**Tab--下载完成
 * @author: merbng
 * @time: 2021/10/15 10:05
 * @description:
 */
class TabDownloadCompleteViewModel : MaterialBaseViewModel() {
    var dataListLD: MutableLiveData<List<DownloadInfo>> = MutableLiveData()
    val userIds: Int by lazy { SpUtils.getInstance().userId }

    //已下载文件 列表
    fun getAlwayDownloadList() {
//        val allFiles = DownloadManager.getInstance().getAllFiles(DownloadManager.FILE_PATH, "")
        val downloadList = DBManager.getInstance().getDownloadList(userIds,false)
        val list = mutableListOf<DownloadInfo>()
        for (file in downloadList) {
            if (file.downloadStatus.equals(DownloadInfo.DOWNLOAD_OVER)) {
                list.add(file)
            }
        }
        //去重
        val map = LinkedHashMap<String, DownloadInfo>()
        for (info in list) {
            if (!map.containsKey(info.path)) {
                map.put(info.path, info);
            }
        }
        val cabList = ArrayList<DownloadInfo>(map.values)
        dataListLD.value = cabList
    }
}